package com.emenu;

import java.io.File;

import java.util.ArrayList;
import java.util.List;
import com.emenu.application.StaticObject;
import com.emenu.bean.Localparm;
import com.emenu.bean.Menu;
import com.emenu.bean.PrintBusRetailDetail;
import com.emenu.bean.PubPrinter;
import com.emenu.bean.PubPrinterTable;
import com.emenu.bean.PubSeries;
import com.emenu.bean.PubSeriesGroup;
import com.emenu.bean.PubTable;
import com.emenu.bean.PubTimer;
import com.emenu.bean.SubMenu;
import com.emenu.bean.User;
import com.emenu.common.CM;
import com.emenu.common.GetFunRightList;
import com.emenu.common.GetWebServiceData;
import com.emenu.common.JsonUntil;
import com.emenu.common.RSStrFromJsonText;
import com.emenu.common.SaveMenuImg;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;

public class Login extends Activity {
	private EditText view_userID;
	private EditText view_userPW;
	private Button button_submit;
	private Button button_cancel;
	private Button button_Set;
 
	
	private Intent intent;
	public static final String PREFS_NAME = "jpVerFile";
	public static final String SET_NAME = "jpSetFile";

	
	StaticObject SO;

	private String tempwsdl;
	private String istest = "0";// 默认正式版
	private String islocaldata = "0";// 数据是否写入sd卡文件
	private SharedPreferences settings;
	
//	private String url;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		findViewsById();
		setListener();

		

		SO = (StaticObject) getApplication();
		settings = getSharedPreferences(SET_NAME, Activity.MODE_WORLD_READABLE
				| Activity.MODE_WORLD_WRITEABLE | Activity.MODE_APPEND);

		tempwsdl = settings.getString("url", "");
		istest = settings.getString("systype", "0");
		islocaldata = settings.getString("localdata", "0");
			

	}
	

	/** 视图 样式 */
	private void findViewsById() {

		view_userID = (EditText) findViewById(R.id.user_id);

		view_userPW = (EditText) findViewById(R.id.user_pw);

		button_submit = (Button) findViewById(R.id.submit);

		button_cancel = (Button) findViewById(R.id.cancel);
		button_Set = (Button) findViewById(R.id.buttonset);

	}

	/** 设置监听器 */
	private void setListener() {
		button_submit.setOnClickListener(submitListener);
		button_cancel.setOnClickListener(cancelListener);
		button_Set.setOnClickListener(setListener);

	}

	/** 提交监听器事件 */
	private OnClickListener setListener = new OnClickListener() {
		@Override
		public void onClick(View v) {

			final String wsdldefault = "http://192.168.1.100/Ws_jpdzcp/WS_DZCP.asmx";

			LayoutInflater factory = LayoutInflater.from(Login.this);
			final View setView = factory.inflate(R.layout.jpsetting, null);
			final EditText wsdladd = (EditText) setView
					.findViewById(R.id.set_wsdl);
			final CheckBox sysckb = (CheckBox) setView
					.findViewById(R.id.ckbsys);
			final CheckBox locckb = (CheckBox) setView
			.findViewById(R.id.ckblocal);

			// SharedPreferences settings = getSharedPreferences(SET_NAME, 0);
			// String s1=settings.getString("url","");
			// int s2=settings.getInt("systype",0);
			wsdladd.setText(tempwsdl);
			if (istest.equals("1")) {
				
				sysckb.setChecked(true);
				
			}
			if (islocaldata.equals("1")) {
				locckb.setChecked(true);
			}

			Button btdefault = (Button) setView.findViewById(R.id.setdefault);
			Button btsave = (Button) setView.findViewById(R.id.setsave);

			btdefault.setOnClickListener(new OnClickListener() {
				public void onClick(View arg0) {
					wsdladd.setText(wsdldefault);
				};
			});

			btsave.setOnClickListener(new OnClickListener() {
				public void onClick(View arg0) {

					// webservice，是否演示版配置
					// SharedPreferences sp = getSharedPreferences(SET_NAME,
					// Activity.MODE_WORLD_READABLE
					// | Activity.MODE_WORLD_WRITEABLE
					// | Activity.MODE_APPEND);

					SharedPreferences.Editor editor = settings.edit();
					editor.putString("url", wsdladd.getText() + "");

					if (sysckb.isChecked()) {
						editor.putString("systype", "1");
						istest = "1";
					} else {
						editor.putString("systype", "0");
					}

					if (locckb.isChecked()) {
						editor.putString("localdata", "1");
						islocaldata = "1";
					} else {
						editor.putString("localdata", "0");
					}

					editor.commit();
				};
			});

			AlertDialog.Builder builder = new AlertDialog.Builder(Login.this);
			builder.setTitle("").setIcon(android.R.drawable.ic_dialog_info)
					.setView(setView)
					// .setPositiveButton(R.string.setdefault, null)
					.setNegativeButton(R.string.close, null).show();

			// builder.setPositiveButton(R.string.close,
			// new DialogInterface.OnClickListener() {
			//
			// @Override
			// public void onClick(DialogInterface dialog, int which) {		
			//
			// }
			// }
			// );

		}
	};

	/** 提交监听器事件 */
	private OnClickListener submitListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			String userCode = String.valueOf(view_userID.getText()).trim();
			String userPW = String.valueOf(view_userPW.getText()).trim();

			if ("".equals(userCode) || "".equals(userPW)) {
				Toast.makeText(Login.this, "账号或密码不能为空", Toast.LENGTH_SHORT)
						.show();
			} else {
				new loginTask().execute(userCode, userPW);
			}
		}
	};

	/** 取消监听器事件 */
	private OnClickListener cancelListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			view_userID.setText("");
			view_userPW.setText("");
		}
	};

	/**
	 * 登陆验证异步处理
	 */
	public class loginTask extends AsyncTask<String, Void, Boolean> {

		// 执行onPreExecute()完后立即调用
		@Override
		protected Boolean doInBackground(String... params) {
			// 调用webService验证用户

			// Boolean isCheck = GetWebServiceData.WSIsUser(params[0],
			// params[1]);
			Boolean isCheck = false;
			for (User user : SO.getUserList()) {

				if (params[0].equals(user.getCode())) {
					if (JsonUntil.isPassword(params[1], user.getPassword())) {
						SO.setStaticUserName(user.getName());
						isCheck = true;
						break;
					}
				}
			}

			if (isCheck) {

				return true;
			}

			return false;
		}

		// doInBackground后执行
		// result 的得自于doInBackgroun
		@Override
		protected void onPostExecute(Boolean result) {

			if (result) {
				SO.setStaticUser(String.valueOf(view_userID.getText()).trim());
				SO.setStaticUserPW(String.valueOf(view_userPW.getText()).trim());

				intent = new Intent();
				intent.setClass(Login.this, MenuTab.class);

				// 启动Activity
				startActivity(intent);
			} else {
				loginerr();
			}
		}

	}
 
	private void loginerr() {

		Toast.makeText(this, "账户或密码不正确", Toast.LENGTH_SHORT).show();

	}

}